# encoding: utf-8
from pyo import *


class Grain:
    def __init__(self):
        
        #self.table = SndTable('/Users/vincentmonastesse/Desktop/Krisnamurti.aiff')
        self.table = SndTable(SNDS_PATH+"/transparent.aif")
        self.sndDur = self.table.getDur()

        self.env = HannTable()
        
        self.grnX = SigTo(value=0.5, time=0.05, init=0.5)
        self.grnY = SigTo(value=0.5, time=0.05, init=0.5)
        self.grnZ = Sig(0.001)  # Correspond au parametre 'z' de l'interface tactile
        
        self.phase = Phasor(freq=self.table.getRate()*self.grnX, phase=0, mul=self.table.getSize(), add=0)

        self.dur = Noise(mul=Clip(self.grnZ, min=0, max=1.), add=.1)

        self.grain = Granulator( table=self.table,    
                            env=self.env,        
                            pitch=self.grnY,      
                            pos=self.phase,        
                            dur=self.dur,        
                            grains=40,      
                            basedur=.1,      
                            mul=.1).mix(2)
                            
    def changeTable(self, snd):
        self.table.sound = snd
    
    def changeX(self, x):
        self.grnX.value = x

    def changeY(self, y):
        self.grnY.value = y
    
    def grainDur(self,z):
        self.grnZ.value = z

    def sig(self):
        return self.grain

class Fx:
    def __init__(self,input):
        
        self.input = input
        
        self.fxX = SigTo(value=0.5, time=0.05, init=0.5)
        self.fxY = SigTo(value=0.5, time=0.05, init=0.5)
        self.fxZ = SigTo(0)
        
        lfo = Sine(freq=.2, mul=.25,add=.25)

        self.band = FourBand(self.input,freq1=150*self.fxY, freq2=300*self.fxY, freq3=600*self.fxY, mul=1,add=0)
        self.dl = Delay(self.band,delay=[4,3,2,1]*self.fxX,feedback=self.fxZ,mul=1).mix(2).out()
        
       
        
    def changeX(self, x):
        self.fxX.value = x

    def changeY(self, y):
        self.fxY.value = y
        
    def changeZ(self, z):
        self.fxZ.value = z
        
    def setFreq1(self, x):
        self.band.setFreq1(x)
        
    def setFreq2(self, x):
        self.band.setFreq2(x)
        
    def setFreq3(self, x):
        self.band.setFreq3(x) 
        
    
        
   
